// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SourceFile

package com.sgame.android.network;

import com.sgame.android.MLog;
import com.sgame.android.lang.AppVersion;
import com.sgame.android.network.util.*;
import com.sgame.android.network.InvalidCredentialsConfigurationException;
import com.sgame.android.network.Credentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URLEncodedUtils;

// Referenced classes of package com.mobage.ww.android.network:
//            c, HttpRequest, b

public final class SgameHttpClient extends ExecuteHttpRequest
{

    protected SgameHttpClient(Credentials credentials, CookieStore cookiestore, int flags, String session, String storeVisitId, String s2, String s3, String s4)
        throws InvalidCredentialsConfigurationException
    {
        if(credentials != null ? credentials.getConsumerKey() != null || credentials.getConsumerSecret() != null || (credentials.getOAuthToken() != null && credentials.getOAuthToken().length() != 0) || credentials.getOAuthTokenSecret() != null && credentials.getOAuthTokenSecret().length() != 0 ? credentials.getConsumerKey().length() == 0 || credentials.getConsumerSecret().length() == 0 : true : true)
            throw new InvalidCredentialsConfigurationException();
        mCredentials = credentials;
        mMakeRequestUrl = new MakeRequestUrl(credentials);
        mSession = session;
        if(cookiestore != null)
            setCookieStore(cookiestore);
        if(flags == 2)
        	mOAuth2Token = credentials.getOAuth2Token();
        setUserAgent((new StringBuilder()).append(s2).append("/").append(s3).append(" Mobage/").append(s4).append(" ").append(AppVersion.a()).toString());
        mFlags = flags;
        mStoreVisitId = storeVisitId;
    }

    private String getStringParams(HttpRequest httprequest) //a
    {
        ArrayList<PairValue> arraylist = new ArrayList<PairValue>(httprequest.getQueryParams());
        if(httprequest.getBody() != null && (httprequest.getBody() instanceof UrlEncodedFormEntity))
        {
            Object obj = (UrlEncodedFormEntity)httprequest.getBody();
            Iterator<NameValuePair> item;
            try
            {
                NameValuePair namevaluepair;
                for(item = URLEncodedUtils.parse(((org.apache.http.HttpEntity) (obj))).iterator(); item.hasNext(); arraylist.add(new PairValue(namevaluepair.getName(), namevaluepair.getValue() != null ? ((Object) (namevaluepair.getValue())) : "")))
                    namevaluepair = item.next();

            }
            catch(IOException ioexception)
            {
                throw new RuntimeException("Unable to read request body", ioexception);
            }
        }
        return mMakeRequestUrl.getStringParams(httprequest.getMethod(), (new StringBuilder()).append(getUrl()).append(httprequest.getPath()).toString(), arraylist);
    }

    public final void setToken(String s) //c
    {
    	mOAuth2Token = s;
    }

    final void InitHeader(HttpUriRequest httpurirequest) //a
    {
        super.InitHeader(httpurirequest);
        if(mSession != null)
            httpurirequest.addHeader("X-Stat-Session", mSession);
        if(mStoreVisitId != null)
        {
        	MLog.d("MobageHttpClient", (new StringBuilder("Sending X-Bank-StoreVisitId: ")).append(mStoreVisitId).toString());
            httpurirequest.addHeader("X-Bank-StoreVisitId", mStoreVisitId);
        }
    }

    public final void Execute(HttpRequest httprequest, AsyncHttpResponseHandler parseHandler) //a
    {
        mMakeRequestUrl = new MakeRequestUrl(mCredentials);
        HttpRequest httprequest1 = httprequest;
        String obj;
        if(mFlags == 2)
        {
            obj = (new StringBuilder("bearer: ")).append(mOAuth2Token).toString();
        } else
        {
            if(mFlags != 1)
            {
            	MLog.d("MobageHttpClient", (new StringBuilder("Unimplemented oauth version used: ")).append(mFlags).append(". Default to v1").toString());
            	mFlags = 1;
            }
            obj = getStringParams(httprequest1);
        }
        httprequest.setHeader("Authorization", obj);
        MLog.d("MobageHttpClient", (new StringBuilder("Authorization Header: ")).append(obj).toString());
        obj = Locale.getDefault().getLanguage();
        MLog.d("MobageHttpClient", (new StringBuilder("MobageHttpClient - Using locale: ")).append(obj).toString());
        if(((String) (obj)).length() == 0)
        {
            obj = "en";
            MLog.e("MobageHttpClient", "MobageHttpClient - Unable to get default locale for this device, using en instead");
        }
        httprequest.setHeader("Accept-Language", ((String) (obj)));
        super.Execute(httprequest, parseHandler);
    }

    private int mFlags;
    private MakeRequestUrl mMakeRequestUrl;
    private String mOAuth2Token;
    private Credentials mCredentials;
    private String mSession;
    private String mStoreVisitId;
}
